package Smt.ServerImp;

import Smt.BaseDao.SupplierDao;
import Smt.BaseDaoImp.SupplierDaoImp;
import Smt.Server.SupplierServer;
import Smt.Util.Supplier;

import java.util.ArrayList;

public class SupplierServerImp implements SupplierServer {
	SupplierDao ur = new SupplierDaoImp();
	private int yenum;
	private int SupplierCount;

	public int getSupplierCount() {
		return SupplierCount;
	}

	public void setSupplierCount(int supplierCount) {
		SupplierCount = supplierCount;
	}

	public int getYenum() {
		return yenum;
	}

	public void setYenum(int yenum) {
		this.yenum = yenum;
	}

	//查询供应商列表
	public ArrayList<Supplier> getSupplier() {
		Object[] o = null;
		String sql = "select * from Supplier ORDER by id asc";
		return ur.getSupplierList(o, sql);
	}

	//name供应商总数
	public int SupplierCount(String p, String d) {
		return bd.SupplierCount(p, d);
	}

	public ArrayList<Supplier> Read(int y, int ye, String productName, String descriptionName) {
		this.SupplierCount = SupplierCount(productName, descriptionName);
		this.yenum = SupplierCount / ye;//总页数
		if (SupplierCount % ye != 0)
			this.yenum++;
		int tou = (y - 1) * ye + 1;//头记录
		int wei = y * ye;//尾记录
		String sql = "select * from (select id,name,description,atten,tel,adds,@rownum:=@rownum+1 rn from Supplier,(select @rownum:=0) t where name like ? and description like ? ORDER by id) a where a.rn>=? and a.rn<=?";
		Object[] o = {"%" + productName + "%", "%" + descriptionName + "%", tou, wei};
		return ur.getSupplierList(o, sql);
	}

	//通讯录的增加
	public boolean add(Supplier Supplier) {
		String sql = "insert into Supplier(name,description,atten,tel,adds) values(?,?,?,?,?)";
		Object[] o = {Supplier.getName(), Supplier.getDescription(), Supplier.getAtten(), Supplier.getTel(), Supplier.getAdd()};
		return bd.util(sql, o);
	}

	//通讯录的删除
	public boolean remove(int id) {
		Object[] o = {id};
		String sql = "delete from Supplier where id=?";
		return bd.util(sql, o);
	}

	//通讯录的更改
	public boolean update(Supplier Supplier) {

		String sql = "update Supplier set name=?,description=?,atten=?,tel=?,adds=? where id=?";
		Object[] o = {Supplier.getName(), Supplier.getDescription(), Supplier.getAtten(), Supplier.getTel(), Supplier.getAdd(), Supplier.getId()};
		return bd.util(sql, o);
	}
}
